package com.atguigu.gulimail.order.web;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.utils.R;
import com.atguigu.gulimail.order.entity.OrderEntity;
import com.atguigu.gulimail.order.service.OrderService;
import com.atguigu.gulimail.order.vo.OrderConfirmVo;
import com.atguigu.gulimail.order.vo.OrderSubmitResponseVo;
import com.atguigu.gulimail.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;
import java.util.concurrent.ExecutionException;


@Controller
public class WebOrderController {

    @Autowired
    OrderService orderService;


    @ResponseBody
    @GetMapping("/getOrderWithOrderSn/{orderSn}")
    public R getOrder(@PathVariable("orderSn")String orderSn){
       OrderEntity orderEntity= orderService.getOrderWithOrderSn(orderSn);
                return R.ok().setDate(orderEntity);
    }

    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo=orderService.confirmOrder();
        model.addAttribute("confirmVo",confirmVo);
        return "confirm";

    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, RedirectAttributes redirectAttributes,Model model){

        //根据页面提交过来的令牌信息。然后判定订单是否创建成功。
            OrderSubmitResponseVo responseVo= orderService.submitOrder(vo);
            if (responseVo.getCode()==0){
                //code=0表示订单创建成功。
                model.addAttribute("responseVo",responseVo);
                System.out.println("aaa");
                return "pay";
//                redirectAttributes.addFlashAttribute("success", responseVo);
//                Map<String, ?> flashAttributes = redirectAttributes.getFlashAttributes();
//
//                return "redirect:http://order.gulimail.com/submitSuccess";

            }else{
                String msg="下单失败";
                switch (responseVo.getCode()){
                    case 1: msg+="信息过期，重新提交";break;
                    case 2: msg+="价格发生变化";break;
                    case 3: msg+="没有库存";break;
                }
                redirectAttributes.addFlashAttribute("msg",msg);
                System.out.println(msg);

                //失败 重定向到订单页
                return "redirect:http://order.gulimail.com/toTrade";
            }
    }
//    @RequestMapping("/submitSuccess")
//    public String submitSuccess(OrderSubmitResponseVo vo, RedirectAttributes redirectAttributes,Model model){
//
//        Map<String, ?> flashAttributes = redirectAttributes.getFlashAttributes();
//        OrderSubmitResponseVo responseVo = (OrderSubmitResponseVo) flashAttributes.get("success");
//
//       // OrderSubmitResponseVo parse = (OrderSubmitResponseVo) JSON.parse(responseVo);
//        model.addAttribute("responseVo",responseVo);
//        return "pay";
//    }

}
